package com.asia5b.a5mall.member.activity.comment

import android.app.Activity
import android.content.Intent
import androidx.core.content.ContextCompat
import androidx.recyclerview.widget.LinearLayoutManager
import android.text.TextUtils
import android.view.View
import android.widget.TextView
import androidx.lifecycle.Observer
import com.alibaba.android.arouter.facade.annotation.Route
import com.asia5b.a5bmall.resource.ActCons
import com.asia5b.a5mall.member.R
import com.asia5b.a5mall.member.adapter.CommentAgainAdapter
import com.asia5b.a5mall.member.databinding.ActivityCommentagainBinding
import com.asia5b.a5mall.middleware.base.SimpleActivity
import com.asia5b.a5mall.middleware.event.PostCommentEvent
import com.asia5b.a5mall.middleware.logic.viewmodel.member.CommentAgainActivityVM
import com.asia5b.a5mall.middleware.model.member.CommentsModel
import com.asia5b.a5mall.middleware.model.bean.PostCommentBean
import com.asia5b.a5mall.middleware.model.bean.PostGoodsBean
import com.asia5b.a5mall.middleware.utils.*
import com.asia5b.a5mall.middleware.widget.CancelSureViewModel
import com.luck.picture.lib.PictureSelector
import com.luck.picture.lib.entity.LocalMedia
import kotlinx.android.synthetic.main.activity_commentagain.*

/**
 * Created by zhangyuncai on 2019/12/21.
 * @Note   追加评价
 */
@Route(path = ActCons.CommentAgainActivity)
class CommentAgainActivity : SimpleActivity<CommentAgainActivityVM, ActivityCommentagainBinding>() {
    override val viewModelClazz: Class<CommentAgainActivityVM>
        get() = CommentAgainActivityVM::class.java

    override fun getDataBinding(): ActivityCommentagainBinding {
        return ActivityCommentagainBinding.inflate(layoutInflater)
    }
    override var sIsDark = true
    val mRecyclerView by lazy { rv_commentagain }
    val mDatas = arrayListOf<CommentsModel>()
    val mAdapter by lazy { CommentAgainAdapter(mDatas) }
    val titleRightText by lazy { findViewById<TextView>(R.id.tv_title_righttxt) }


    override fun initView() {
        mRecyclerView.run {
            layoutManager = LinearLayoutManager(mActivity)!!
            adapter = mAdapter
        }
        if (intent.hasExtra("datas")) {
            val list = intent.getSerializableExtra("datas") as ArrayList<CommentsModel>
            if (!list.isNullOrEmpty()) {
                mDatas.addAll(list)
                mAdapter.notifyDataSetChanged()
                setSubmitState()
            }
        } else if (intent.hasExtra("order_sn")) {
            val order_sn = intent.getStringExtra("order_sn")
            mViewModel.getFirstCommentDetail(order_sn!!)
        }
    }

    var allCommenState = true
    fun setSubmitState() {
        for (commentsModel in mDatas) {
            if (!TextUtils.equals("PASS_AUDIT", commentsModel.audit_status) && allCommenState) {//审核通过
                allCommenState = false
            }
        }
        if (allCommenState) {
            titleRightText.setOnClickListener {
                commit()
            }
            titleRightText.setTextColor(ContextCompat.getColor(mActivity, R.color.common_text_blue))
        } else {
            titleRightText.setTextColor(ContextCompat.getColor(mActivity, R.color.common_text_gray))
        }
    }

    override fun initEvent() {
    }

    override fun startObserver()
    {
        mViewModel.commitResultLiveData.observe(this, Observer {
            commitResult()
        })
        mViewModel.getFirstCommentDetailResultLiveData.observe(this, Observer {
            getFirstCommentDetailResult(it)
        })
    }

    override fun initData() {

    }

    fun commit() {
        val goods = ArrayList<PostGoodsBean>()
        mDatas.forEach {
            val item = PostGoodsBean()
            item.content = it.commentContent.get()!!
            it.selectionMedia?.forEach {
                item.waitUploadImages.add(it.compressPath)
            }
            item.sku_id = it.sku_id
            item.seller_id = it.seller_id
            item.commentId = it.comment_id
            dddLog("size:${it.selectionMedia?.size}")
            goods.add(item)
        }
        val result = PostCommentBean()
        result.comments = goods

        if (false) {
            return
        }
        mViewModel.commit(result)
    }

     fun commitResult() {
        showMessage(getString(R.string.发表成功))
        getEventBus().post(PostCommentEvent())
        pop()
    }

     fun getFirstCommentDetailResult(list: java.util.ArrayList<CommentsModel>?) {
        if (!list.isNullOrEmpty()) {
            mDatas.addAll(list)
            mAdapter.notifyDataSetChanged()
            setSubmitState()
        }
    }

    override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
        super.onActivityResult(requestCode, resultCode, data)
        if (resultCode != Activity.RESULT_OK)
            return
        var uploadPosition = requestCode
        // 图片选择
        val selectList = PictureSelector.obtainMultipleResult(data)!!
        val selectionMedia = arrayListOf<LocalMedia>()
        selectList.forEach {
            selectionMedia.add(it)
        }
        if (mDatas.size > uploadPosition) {
            mDatas.get(uploadPosition).selectionMedia = selectionMedia
            mAdapter.notifyItemChanged(uploadPosition)
        }
    }

    fun showNoticeDialog() {
        CancelSureViewModel(mActivity).apply {
            contentStringRes=R.string.当前修改尚未提交_是否离开
            leftColorInt=R.color.colorPrimaryDark
            rihghtColorInt=R.color.common_text_gray
            rightClick={
                pop()
            }
        }.show()
    }

    override fun onBackPressed() {
        if (!allCommenState) {
            super.onBackPressed()
        } else {
            showNoticeDialog()
        }
    }

    override fun backClick(v: View) {
        if (!allCommenState) {
            super.backClick(v)
        } else {
            showNoticeDialog()
        }
    }
}